package NovoSISAM.Gui;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.text.ParseException;
import javax.swing.JOptionPane;
import NovoSISAM.Model.Paciente;
import static NovoSISAM.Dao.PacienteDao.*;
import static NovoSISAM.Dao.ValidadorDao.*;
import static NovoSISAM.DataBase.DataBase.*;

/**
 *
 * @author Cleiviane
 */
public class Cadastro_Pacientes extends javax.swing.JInternalFrame {

    Paciente paciente;

    /** Creates new form Cadastro_Pacientes */
    public Cadastro_Pacientes() {
        initComponents();
        ConectarDataBase();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        txtProntuario = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        txtCelular = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtTelefones = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtDNascimento = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtCpf = new javax.swing.JTextField();
        txtNome = new javax.swing.JTextField();
        txtDoencas = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        cboSituacao = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        txtEndereco = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaDadosClin = new javax.swing.JTextArea();
        jLabel8 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        botaoSalvar = new javax.swing.JButton();
        botaoCancelar = new javax.swing.JButton();
        botaoNovo = new javax.swing.JButton();
        botaoExcluir = new javax.swing.JButton();
        botaoAlterar = new javax.swing.JButton();

        setClosable(true);
        setMaximizable(true);
        setTitle("Pacientes - SISAM");

        jLabel2.setText("Nome:");

        txtProntuario.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtProntuarioFocusLost(evt);
            }
        });

        jLabel1.setText("Prontuário:");

        jLabel10.setText("Celular:");

        txtTelefones.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtTelefonesFocusLost(evt);
            }
        });

        jLabel9.setText("Telefone:");

        jLabel11.setText("(somente números)");

        jLabel5.setText("Endereço:");

        jLabel4.setText("D. Nasc:");

        jLabel3.setText("CPF:");

        txtCpf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCpfActionPerformed(evt);
            }
        });

        txtNome.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtNomeFocusLost(evt);
            }
        });

        txtDoencas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDoencasActionPerformed(evt);
            }
        });

        jLabel6.setText("Doenças Diag:");

        cboSituacao.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione a Situação do Paciente", "Em Tratamento", "Tratamento Finalizado", "Aguardando Retorno", "Aguardando Resultado Exame", "Tratamento Interrompido", "Obito" }));

        jLabel7.setText("Situação:");

        txtEndereco.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtEnderecoActionPerformed(evt);
            }
        });

        txaDadosClin.setColumns(20);
        txaDadosClin.setRows(5);
        jScrollPane1.setViewportView(txaDadosClin);

        jLabel8.setText("Dados Clínic:");

        jLabel12.setText("DD/MM/AAAA");

        jLabel13.setText("(somente números)");

        jLabel14.setText("(somente números)");

        botaoSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/NovoSISAM/Gui/Imagens/salvar.jpg"))); // NOI18N
        botaoSalvar.setText("Salvar");
        botaoSalvar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botaoSalvar.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        botaoSalvar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        botaoSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoSalvarActionPerformed(evt);
            }
        });

        botaoCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/NovoSISAM/Gui/Imagens/cancelar.jpg"))); // NOI18N
        botaoCancelar.setText("Cancelar");
        botaoCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botaoCancelar.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        botaoCancelar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        botaoCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoCancelarActionPerformed(evt);
            }
        });

        botaoNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/NovoSISAM/Gui/Imagens/novo.jpg"))); // NOI18N
        botaoNovo.setText("Novo");
        botaoNovo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botaoNovo.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        botaoNovo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        botaoNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoNovoActionPerformed(evt);
            }
        });

        botaoExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/NovoSISAM/Gui/Imagens/excluir.jpg"))); // NOI18N
        botaoExcluir.setText("Excluir");
        botaoExcluir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botaoExcluir.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        botaoExcluir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        botaoExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoExcluirActionPerformed(evt);
            }
        });

        botaoAlterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/NovoSISAM/Gui/Imagens/alterar.jpg"))); // NOI18N
        botaoAlterar.setText("Alterar");
        botaoAlterar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botaoAlterar.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        botaoAlterar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        botaoAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoAlterarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jLabel9)
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtTelefones, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                            .addComponent(txtDNascimento, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(jLabel13))
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtCelular, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                            .addComponent(txtCpf, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel12)
                            .addComponent(jLabel14)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(botaoNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(botaoSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(botaoExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(botaoAlterar)
                            .addGap(14, 14, 14)
                            .addComponent(botaoCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 557, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(25, 25, 25))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel5))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGap(22, 22, 22))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel8)
                                .addComponent(jLabel7))
                            .addGap(26, 26, 26)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(1, 1, 1)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(cboSituacao, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtDoencas, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtEndereco, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 561, Short.MAX_VALUE)))
                        .addComponent(txtNome, javax.swing.GroupLayout.DEFAULT_SIZE, 562, Short.MAX_VALUE)
                        .addComponent(txtProntuario))
                    .addContainerGap()))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {botaoAlterar, botaoCancelar, botaoExcluir, botaoNovo, botaoSalvar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(73, 73, 73)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtDNascimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(txtTelefones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(txtCelular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13)
                    .addComponent(jLabel14))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 133, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(botaoExcluir)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(botaoCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(botaoNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(botaoAlterar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(botaoSalvar, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)))
                .addGap(26, 26, 26))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtProntuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1))
                    .addGap(9, 9, 9)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jLabel3)
                    .addGap(44, 44, 44)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel5))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtDoencas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cboSituacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel7))
                    .addGap(57, 57, 57)
                    .addComponent(jLabel8)
                    .addContainerGap()))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {botaoAlterar, botaoCancelar, botaoExcluir, botaoNovo, botaoSalvar});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtCpfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCpfActionPerformed
}//GEN-LAST:event_txtCpfActionPerformed

    private void txtDoencasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDoencasActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtDoencasActionPerformed

    private void txtEnderecoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtEnderecoActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtEnderecoActionPerformed

    private void botaoSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoSalvarActionPerformed
        paciente = new Paciente();
        paciente.setNome(txtNome.getText());
        paciente.setProntuario(Integer.parseInt(txtProntuario.getText()));
        paciente.setCpf(txtCpf.getText());
        paciente.setEndereco(txtEndereco.getText());
        paciente.setDoencas(txtDoencas.getText());
        paciente.setSituacao(cboSituacao.getSelectedIndex());
//            Date dataformat = null;
//            try {
//                dataformat = convertStringPraDate(txtDNascimento.getText());
//            } catch (ParseException ex) {
//                Logger.getLogger(Cadastro_Paciente.class.getName()).log(Level.SEVERE, null, ex);
//            }
        //paciente.setData_nasc(dataformat);
        paciente.setData_nasc(txtDNascimento.getText());
        paciente.setTelefone(txtTelefones.getText());
        paciente.setCelular(txtCelular.getText());
        paciente.setDadosClin(txaDadosClin.getText());

        if (txtProntuario.isEditable()) {
            if (!procuraCampoVazio()) {
                if (inserirPaciente(paciente)) {
                    JOptionPane.showMessageDialog(null, "Paciente cadastrado com sucesso.");
                    limparCampos();
                    destravaCampos();
                    botaoAlterar.setEnabled(false);
                    botaoCancelar.setEnabled(false);
                    botaoExcluir.setEnabled(false);
                    botaoNovo.setEnabled(true);
                    botaoSalvar.setEnabled(true);
                } else {
                    JOptionPane.showMessageDialog(null, "Erro ao Inserir o Paciente.");
                }
            } else {
                JOptionPane.showMessageDialog(null, "Existe um campo vazio.Por favor verifique.");
            }
        } else {
            alterarPaciente(paciente);
            travaCampos();
            botaoAlterar.setEnabled(true);
            botaoCancelar.setEnabled(true);
            botaoExcluir.setEnabled(true);
            botaoNovo.setEnabled(true);
            botaoSalvar.setEnabled(false);
        }
}//GEN-LAST:event_botaoSalvarActionPerformed

    private void botaoCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoCancelarActionPerformed
        if (txtProntuario.getText().isEmpty()) {
            Cadastro_Pacientes.this.dispose();
            botaoAlterar.setEnabled(false);
            botaoCancelar.setEnabled(false);
            botaoExcluir.setEnabled(false);
            botaoNovo.setEnabled(true);
            botaoSalvar.setEnabled(false);
            destravaCampos();
        } else {
            botaoAlterar.setEnabled(true);
            botaoCancelar.setEnabled(false);
            botaoExcluir.setEnabled(true);
            botaoNovo.setEnabled(true);
            botaoSalvar.setEnabled(false);
            travaCampos();
        }
}//GEN-LAST:event_botaoCancelarActionPerformed

    private void botaoNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoNovoActionPerformed
        limparCampos();
        destravaCampos();
        txtProntuario.requestFocus();
        botaoAlterar.setEnabled(false);
        botaoCancelar.setEnabled(true);
        botaoExcluir.setEnabled(false);
        botaoNovo.setEnabled(false);
        botaoSalvar.setEnabled(true);
}//GEN-LAST:event_botaoNovoActionPerformed

    private void botaoExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoExcluirActionPerformed
        if (paciente == null) {
            JOptionPane.showMessageDialog(this, "Selecione alguem para excluir!");
            return;
        } else {
            boolean result = JOptionPane.showInternalConfirmDialog(this, "Deseja realmente excluir esse cadastro?") == 0;
            if (result == true) {
                excluirPaciente(paciente.getProntuario());
                JOptionPane.showMessageDialog(this, "Paciente Excluído com sucesso!");
                txtProntuario.requestFocusInWindow();
            } else {
                return;
            }
        }
}//GEN-LAST:event_botaoExcluirActionPerformed

    private void botaoAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoAlterarActionPerformed
        this.destravaCampos();
        txtProntuario.setEditable(false);
        botaoAlterar.setEnabled(false);
        botaoCancelar.setEnabled(true);
        botaoExcluir.setEnabled(true);
        botaoNovo.setEnabled(false);
        botaoSalvar.setEnabled(true);
}//GEN-LAST:event_botaoAlterarActionPerformed

    private void txtProntuarioFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtProntuarioFocusLost
        int prontuario = Integer.parseInt(txtProntuario.getText());
        try {
            paciente = localizarPacientePorCodigo(prontuario);
        } catch (ParseException ex) {
            Logger.getLogger(Cadastro_Pacientes.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (paciente != null) {
            preencheCampos(paciente);
        }
    }//GEN-LAST:event_txtProntuarioFocusLost

    private void txtNomeFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtNomeFocusLost
        String nome = txtNome.getText();
        try {
            paciente = localizarPacientePorNome(nome);
        } catch (ParseException ex) {
            Logger.getLogger(Cadastro_Pacientes.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (paciente != null) {
            preencheCampos(paciente);
        }
    }//GEN-LAST:event_txtNomeFocusLost

    private void txtTelefonesFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtTelefonesFocusLost
         int result = validaCampoNumerico(txtTelefones.getText());
            if (result != 0) {
                JOptionPane.showMessageDialog(null, "Este campo só aceita numeros.");
                txtTelefones.setText("");
            }
    }//GEN-LAST:event_txtTelefonesFocusLost

    private boolean procuraCampoVazio() {
        if (txtCelular.getText().equals("") || txtCpf.getText().equals("") || txtDNascimento.getText().equals("") || txtDoencas.getText().equals("")
                || txtEndereco.getText().equals("") || txtNome.getText().equals("") || txtProntuario.getText().equals("") || txtTelefones.getText().equals("")) {
            return true;
        } else {
            return false;
        }
    }

    private void limparCampos() {
        txtProntuario.setText(null);
        txtNome.setText(null);
        txtCpf.setText(null);
        txtEndereco.setText(null);
        txtDoencas.setText(null);
        cboSituacao.setSelectedItem(null);
        txtDNascimento.setText(null);
        txtTelefones.setText(null);
        txaDadosClin.setText(null);
        txtCelular.setText(null);
    }

    private void travaCampos() {
        txtProntuario.setEditable(false);
        txtNome.setEditable(false);
        txtCpf.setEditable(false);
        txtEndereco.setEditable(false);
        txtDoencas.setEditable(false);
        cboSituacao.setEditable(false);
        txtDNascimento.setEditable(false);
        txtTelefones.setEditable(false);
        txaDadosClin.setEditable(false);
        txtCelular.setEditable(false);
    }

    private void destravaCampos() {
        txtProntuario.setEditable(true);
        txtNome.setEditable(true);
        txtCpf.setEditable(true);
        txtEndereco.setEditable(true);
        txtDoencas.setEditable(true);
        cboSituacao.setEditable(true);
        txtDNascimento.setEditable(true);
        txtTelefones.setEditable(true);
        txaDadosClin.setEditable(true);
        txtCelular.setEditable(true);
    }

    private void preencheCampos(Paciente paciente) {
        txtProntuario.setText(String.valueOf(paciente.getProntuario()));
        txtNome.setText(paciente.getNome());
        txtCpf.setText(paciente.getCpf());
        txtEndereco.setText(paciente.getEndereco());
        txtDoencas.setText(paciente.getDoencas());
        cboSituacao.setSelectedIndex(paciente.getSituacao());
        //String dataformat = converteDatePraString(paciente.getData_nasc());
        //txtDNascimento.setText(dataformat);
        txtDNascimento.setText(paciente.getData_nasc());
        txtTelefones.setText(paciente.getTelefone());
        txaDadosClin.setText(paciente.getDadosClin());
        txtCelular.setText(paciente.getCelular());
        this.travaCampos();
        botaoAlterar.setEnabled(true);
        botaoCancelar.setEnabled(false);
        botaoExcluir.setEnabled(true);
        botaoNovo.setEnabled(true);
        botaoSalvar.setEnabled(false);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoAlterar;
    private javax.swing.JButton botaoCancelar;
    private javax.swing.JButton botaoExcluir;
    private javax.swing.JButton botaoNovo;
    private javax.swing.JButton botaoSalvar;
    private javax.swing.JComboBox cboSituacao;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txaDadosClin;
    private javax.swing.JTextField txtCelular;
    private javax.swing.JTextField txtCpf;
    private javax.swing.JTextField txtDNascimento;
    private javax.swing.JTextField txtDoencas;
    private javax.swing.JTextField txtEndereco;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextField txtProntuario;
    private javax.swing.JTextField txtTelefones;
    // End of variables declaration//GEN-END:variables
}
